import { assert } from "../common/assert.js";
import { isArray, isString } from "../data-type/is.js";

/**
 * 统计数组一个键对应值出现次数
 * @param {Array} data 
 * @param {string} key 
 */
export function summaryValueTimes(data, key, hook) {
    hook && hook()
    assert(isArray(data), 'argument[0] not an array');
    assert(isString(key), 'argument[1] not a string');
    let result = {}
    data.forEach(item => {
        let value = item[key];
        if (value in result) {
            return result[value]++
        }

        result[value] = 1;
    })

    return result
}